﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace office_tools
{
    class Program
    {
        static void Main(string[] args)
        {
            //RemoveCss();
            ReplacePic();

        }
        /*业务*/
        
        //修改图片
        private static void ReplacePic(){
            string content=Tools.GetContent();
            //处理PageSpeed
            Regex re=new Regex(@"http://localhost/resource/images/([\s\S]+?)\.([\s\S]+?) ");
            Match m=re.Match(content);
            string sourceFileRoot=@"C:\Users\Administrator\AppData\Local\Temp\page-speed";
            string saveFileRoot=@"D:\yangbo\office_tools_root\Files";
            
            StreamWriter sw=new StreamWriter(@"D:\yangbo\office_tools_root\output.txt");

            while(m.Success){
                string fileName=m.Groups[1].Value;
                string fileExt=m.Groups[2].Value;
                string[] fileNames= Directory.GetFiles(sourceFileRoot,fileName+"_????????????????????????????????.*");
                
                if(fileNames.Length>0){
                    string fileExtNow=fileNames[0].Substring(fileNames[0].IndexOf('.')+1,fileNames[0].Length-fileNames[0].IndexOf('.')-1);
                    File.Copy(fileNames[0],saveFileRoot+"\\"+ fileName+"."+fileExt);
                    if (fileExtNow!=fileExt){                       
                        sw.WriteLine(fileName+"."+fileExt+"--->"+fileName+"."+fileExtNow);
                        sw.Flush();                            
                    }
                    Console.WriteLine(fileNames[0]);
                }
                m=m.NextMatch();
            }
            sw.Close();
        }

        //去掉多余的Css
        private static void RemoveCss(){
            string content=Tools.GetContent();
            string strCss=string.Empty;
            string rules=string.Empty;
            Match mRules= Regex.Match(content,@"--rules--([\s\S]+?)--rules-->");
            if (mRules.Success){
                rules=mRules.Groups[1].Value;
            }
            //Console.WriteLine(rules);
            Match mCss=Regex.Match(content,@"--css--([\s\S]+?)--css-->");
            if (mCss.Success){
                strCss=mCss.Groups[1].Value;
            }
            //Console.WriteLine(strCss);

            Regex re1=new Regex(@"^([\s\S]+?) line",RegexOptions.Multiline);
            Match m1=re1.Match(rules);
            while (m1.Success){
                string rule=m1.Groups[1].Value;
                m1=m1.NextMatch();
                //string pattern=@"^("+rule.Replace(".","\\.")+@"[\s\S]+?)$";
                strCss=Regex.Replace(strCss,@"^("+rule.Replace(".","\\.")+@"[\s\S]+?)[\n\r]+?$","/*auto comment $1 */",RegexOptions.Multiline);
                Console.WriteLine(rule);
            }
            StreamWriter sw=Tools.GetSW();
            sw.Write(strCss);
            sw.Close();        
        }
    }


    /*函数*/
    public static class Tools{
        /// <summary>
        /// 取得输入内容
        /// </summary>
        /// <returns></returns>
        public static string GetContent(){
            StreamReader sr=new StreamReader(@"D:\yangbo\office_tools_root\input.txt");
            string str=sr.ReadToEnd();
            sr.Close();
            return str;
        }
        /// <summary>
        /// 取得StreamWrite
        /// </summary>
        /// <returns></returns>
        public static StreamWriter GetSW(){
            StreamWriter sw=new StreamWriter(@"D:\yangbo\office_tools_root\output.txt");
            return sw;
        }

    }

    
}
